const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const app = express();
const passport = require("passport");

const users = require("./routes/api/users");
const profiles = require("./routes/api/profiles");

//使用body-parser中间件
app.use(bodyParser.json({limit: '10mb'}));
app.use(bodyParser.urlencoded({limit: '10mb',extended:false}));

//引用数据库db的仓库地址配置
const db = require("./config/keys").mongoURI;
//连接数据库
mongoose.connect(db)
  .then(() => {console.log("MongoDB Connected")})
  .catch((err) => {console.log(err)});
// findOneAndUpdate()内部会使用findAndModify驱动，驱动即将被废弃,所以全局设置mongoose：
mongoose.set('useFindAndModify', false);

// app.get("/", (req, res) => {
//   res.send("Hello World!");
// });

//初始化passport
app.use(passport.initialize());
require("./config/passport")(passport);
//这里写法意思：将这里初始化好的passport作为参数传入"./config/passport"文件中的exports函数里
//并且执行这个函数/方法/js

// 使用路由
app.use("/api/users", users);
app.use("/api/profiles", profiles);

app.post("/api/updataImg",(req, res) => {
  res.json({msg: "img is ok"});
});

const port = process.env.PORT || 5000;

app.listen(port, () => {
  console.log(`Server is running on ${port}`);
});